<?php
/**
 * Created by PhpStorm.
 * User: idcu
 * Date: 2016/3/1
 * Time: 13:08
 */
namespace Crm\Model;
use IDCU\Model\RelationModel;

class CommunityModel extends RelationModel
{
    protected $_validate = array(
        array('no','require','小区编号必填',1,'regex',3),
        array('name','require','小区名称必填',1,'regex',1),
        array('province','require','省市必填',1,'regex',1),
        array('city','require','城市必填',1,'regex',1),
        array('area','require','区县必填',1,'regex',3),
        array('address','require','地址必填',1,'regex',1),
        array('no','/^\d{4}$/','小区编码格式不正确',0,'regex',3),
        array('no,area','checkNo1','小区编码已存在',0,'callback',1),
        array('no,id,area','checkNo2','小区编码已存在',0,'callback',2),
        array('svr_start_time','/^\d{4}-\d{2}-\d{2}$/','时间格式不正确',0,'regex'),
        array('svr_end_time','/^\d{4}-\d{2}-\d{2}$/','时间格式不正确',0,'regex'),
        array('svr_start_time,svr_end_time','checkSvrTime','开始时间不能小于结束时间',0,'callback',3),
    );

    protected $_auto = array();

    protected $_link = array(
        'Zone' => array(
            'mapping_type' => self::HAS_MANY,
            'class_name' => 'Zone',
            'mapping_name' => 'zones',
            'foreign_key' => 'community_id'
        ),
        'Building' => array(
            'mapping_type' => self::HAS_MANY,
            'class_name' => 'Building',
            'mapping_name' => 'buildings',
            'foreign_key' => 'community_id'
        ),
        'Unit' => array(
            'mapping_type' => self::HAS_MANY,
            'class_name' => 'Unit',
            'mapping_name' => 'units',
            'foreign_key' => 'community_id'
        ),
        'Room' => array(
            'mapping_type' => self::HAS_MANY,
            'class_name' => 'Room',
            'mapping_name' => 'rooms',
            'foreign_key' => 'community_id'
        ),
    );

    protected function _before_write(&$data)
    {
        parent::_before_write($data); // TODO: Change the autogenerated stub
        //自动获取小区编码
        if($data['area'] && $data['no']){
            $data['no'] = $data['area'].$data['no'];
        }
        if($data['province'] && $data['city'] && $data[area]){
            //自动生成上级全地址
            $data['full_parent_name'] = D('Region')->getRegion($data['province'])['area_name'].D('Region')->getRegion($data['city'])['area_name'].D('Region')->getRegion($data['area'])['area_name'];
        }
    }

    protected function _after_select(&$resultSet, $options)
    {
        parent::_after_select($resultSet, $options); // TODO: Change the autogenerated stub
        D('Dict')->dictTransform($resultSet,array('type'));
        foreach($resultSet as $k=>$v){
            $status = compareNumber(time(),strtotime($v['svr_start_time']),strtotime($v['svr_end_time']));
            if($status ==  '<'){
                $resultSet[$k]['inSvr'] = '准备入驻';
            }elseif($status ==  '>'){
                $resultSet[$k]['inSvr'] = '停止服务';
            }else{
                $resultSet[$k]['inSvr'] = '正在服务';
            }
        }
    }

    function checkSvrTime($param){
        $result = compareNumber($param['svr_start_time'],$param['svr_end_time']);
        if($result == '<'){
            return true;
        }else{
            return false;
        }
    }

    function checkNo1($param){
        $communities = $this->field('no')->where(array('area'=>$param['area']))->select();
        foreach($communities as $k=>$v){
            if(substr($v['no'],-4) == $param['no']){
                return false;
            }
        }
        return true;
    }

    function checkNo2($param){
        $communities = $this->field('no')->where(array('area'=>$param['area'],'id'=>array('neq',$param['id'])))->select();
        foreach($communities as $k=>$v){
            if(substr($v['no'],-4) == $param['no']){
                return false;
            }
        }
        return true;
    }
}